<?php

namespace App\Models\ExpModels;

use App\Models\ParentModel;
use DB;
use App\Components\Tools;

/**
 * 抖音同步用户视频model
 * @author puppet
 */
class ExpCard extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_exp_ucard';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_exp_ucard';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;

    //获取对应卡卷的信息
    function getCardInfo($id)
    {
        $qurl = Tools::getOneconfig("/config/third.json", array('qiniu', 'qnUrl'));
        $sql = " select a.id,a.open_id,a.writeoff,a.createTime,b.type,b.price,b.url,b.cill,b.remark,b.endValid,b.staValid,c.title,c.talk,c.openId,c.posInurl,c.poiName,c.poiId,c.poiAddress,c.poiCity  from $this->table as a left join pt_exp_card as b on a.cardId= b.id ";
        $sql .= " left join pt_exp_activity as c on a.expId=c.id";
        $sql .= " where a.parnterId=$id  ";
        $re = DB::select($sql);
        foreach ($re as $k => $v) {
            $v['url'] = $qurl . "/" . $v['url'];
          $re[$k] = $v;
            //  return $re;
        }
        return isset($re) ? $re : false;
    }
    //根据活动id和用户id获取对应的数据
    function getAllInfo($avtid){
        $qurl = Tools::getOneconfig("/config/third.json", array('qiniu', 'qnUrl'));
        $sql=" select a.parnterId,a.title,a.talk,a.openId,a.posInurl,a.poiName,a.poiId,a.poiAddress,a.poiCity,a.remark,a.staTime,a.endTime,b.title as cardtitle,b.type,b.price,b.url,b.cill,b.remark,b.staTime,b.endTime,b.staValid,b.endValid,c.open_id from pt_exp_activity as a left join pt_exp_card as b on a.cardId=b.id ";
        $sql.=" left join pt_exp_user as c on a.id=c.expId ";
        $sql.=" where a.id=$avtid";
        $re = DB::select($sql);
        foreach ($re as $k => $v) {
            $v['url'] = $qurl . "/" . $v['url'];
            $re[$k] = $v;
            //  return $re;
        }
        return isset($re) ? $re : false;
    }

    //添加修改视频
    function saveCard($kid,$data){
        if($kid){
            return DB::table('pt_exp_card')->where('id',$kid)->update($data);
        }else{
            $data['createtime']=date('Y-m-d H:i:s');
            return DB::table('pt_exp_card')->insertGetId($data);
        }
    }

    //查询卡券状态
    function selStatus($kid,$partner_id){
        $re=DB::select("select status from pt_exp_card where id={$kid} and parnterId=?",[$partner_id]);
        return isset($re[0]['status']) ? $re[0]['status'] : -1;
    }

    //修改卡券状态
    function upStatus($kid,$data){
        return DB::table('pt_exp_card')->where('id',$kid)->update($data);
    }

    //删除卡券
    function del($kid){
        return DB::table("pt_exp_card")->where('id',$kid)->update(['status'=>-1]);
    }
    //获取卡卷对应的视频链接
    function getvideourl($videoId){
        $qnUrl=Tools::getOneconfig("/config/third.json",array('qiniu','qnUrl'));
        $url=DB::select("select url  from pt_exp_videos where id=?",[$videoId]);
        $imgUrl=$qnUrl."/".$url[0]['url'];
        return isset($imgUrl) ? $imgUrl :false;
    }
    //拉取卡卷使用数据数据20条  $id 传过来活动id直接查询卡卷 根据活动id获取UCARd表信息 然后联表查询用户id和卡卷信息
    function getVIPCardInfo($id,$page=1){
        $user_id=isset(session('vip_user')[0]['admin_id']) ?  intval(session('vip_user')[0]['admin_id']) : 0;
        $size = 20;
        $offset = $size * ($page - 1);
        $count=DB::select("select count(id) as count from pt_exp_ucard where expId=?",[$id]);
        $count=isset($count[0]['count']) ? $count[0]['count'] : 0;
        $sql="select a.cardId,a.videoId,a.createTime,a.usedTime,a.status,a.writeoff,a.parnterId,a.open_id,b.avatar,b.mobile,b.nickname,c.type,c.price,c.url,c.cill,c.remark,c.staValid,c.endValid from pt_exp_ucard as a left join pt_exp_user as b on a.parnterId=b.id";
        $sql.=" left join pt_exp_card as c on a.cardId=c.id ";
        $sql.=" order by a.cardId desc limit {$offset},{$size}";
        $re=DB::select($sql);
        $re=isset($re[0]['cardId']) ? $re : false;
        print_r($re);exit();
        $html="";
        $nextPage=$page < ceil($count/$size) ? $page+1 : 0;
        if($re){
            foreach($re as $k=>$v){
//                $sj = date('Y-m-d',$v['update_time']);
                $html.="<tr>";
                $html.="<td><a href='{$v['avatar']}'><img src='{$v['avatar']}' alt=''></a></td>";
                $html.="<td>{$v['nickname']}</td>";
                $html.="<td><a href=\"https://www.douyin.com/user/{$v['sec_uid']}\" target=\"_blank\"><img src=\"{$v['userImg']}\" width=\"50px\" height=\"50px\"></a></td>";
                $html.="<td style=\"white-space:normal;min-width:150px;word-wrap:break-word;word-break:break-all;\">{$v['content']}</td>";
                $html.="<td>{$v['nickname']}</td>";
                $html.="<td>{$v['user_id']}<br/>".str_replace(["\r\n","\n","\r"],'<br/>',$v['tells'])."</td>";
                $html.="<td><a href=\"{$v['video_url']}\" target=\"_blank\">".mb_substr($v['video_url'],0,20)."...".mb_substr($v['video_url'],-10)."</a></td>";
                $html.="<td>".mb_substr($v['create_time'],0,22)."</td>";
                $html.="<td>".mb_substr($v['update_time'],0,10)."</td>";
                $statusText=$v['status']==0 ? "default" : "checkbox";
                $checkedText=$v['status']==0 ? "" : "checked";
                $html.="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"{$statusText}\" checked=\"{$checkedText}\" onclick=\"upStatus({$v['id']},this)\"><span></span></label></div></td>";
                $follow="snssdk1128://user/profile/".$v['uid'];
                $html.="<td><a href=\"javascript:void(0);\" title=\"扫码私聊\"><span class=\"btn btn-info\" onclick=\"following('{$follow}')\">扫码私聊</span></a></td>";
                $html.="</tr>";
            }
        }
        return ["html"=>$html,'nextPage'=>$nextPage];
    }

    //添加修改视频
    function saveVideo($kid,$data){
        if($kid){
            return DB::table('pt_exp_videos')->where('id',$kid)->update($data);
        }else{
            $data['createtime']=date('Y-m-d H:i:s');
            return DB::table('pt_exp_videos')->insertGetId($data);
        }
    }

    //查询视频状态
    function selVideoStatus($kid,$partner_id){
        $re=DB::select("select status from pt_exp_videos where id={$kid} and parnterId=?",[$partner_id]);
        return isset($re[0]['status']) ? $re[0]['status'] : -1;
    }

    //修改视频状态
    function upVideoStatus($kid,$data){
        return DB::table('pt_exp_videos')->where('id',$kid)->update($data);
    }

    //删除视频
    function Videodel($kid){
        return DB::table("pt_exp_videos")->where('id',$kid)->update(['status'=>-1]);
    }
}